package com.atguigu.mapreduce.flowcount.partition;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @author weiranyi
 * @date 2021/10/25
 * @Description: 输入key：文件偏移量 Long
 * 输入value：当前一行数据
 * 输出key：单词
 * 输出value：给单词打标记1
 */
public class FlowCountMapper extends Mapper<LongWritable, Text, Text, FlowBean> {
    private Text outK = new Text();
    private FlowBean outV = new FlowBean();

    /**
     * @param key     文件偏移量 Long
     * @param value   当前一行数据
     * @param context 上下文对象，作用：程序执行之前产生的结果，以传递给后面的程序
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, FlowBean>.Context context) throws IOException, InterruptedException {
        // 获取一行数据
        String line = value.toString();
        String[] split = line.split("\t");
        // 手机号
        String phone = split[1];
        outK.set(phone);
        String up = split[split.length - 3];
        String down = split[split.length - 2];
        //4 封装outK outV
        outV.setUpFlow(Long.parseLong(up));
        outV.setDownFlow(Long.parseLong(down));
        outV.setSumFlow(); // 类里面写无参方法，求和
        context.write(outK, outV);
    }
}
